package com.show.business.device.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.show.business.common.core.controller.BaseController;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.device.bean.UartTmp;
import com.show.business.device.service.IUartTmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 同步设备报警数据 前端控制器
 * </p>
 *
 * @author dvlp
 * @since 2024-09-30
 */
@Slf4j
@RestController
@RequestMapping("/device/synch")
public class SynchUartTmpController extends BaseController {
    @Resource
    private IUartTmpService uartTmpService;

    /**
     * 机电运维系统同步设备告警数据
     * @param requestParam
     * @return
     */
    @RequestMapping("/synchUartTmp")
    public Object synchUartTmp(@RequestBody JSONObject requestParam) {
        log.info("************synchUartTmp开始执行，时间：{}", DateUtil.now());
        AjaxResult result = decodeVerify(requestParam,true);
        String code = result.get("code").toString();
        if(!StrUtil.equals("200",code)){
            log.info("************synchUartTmp************校验失败");
            return result;
        }
        //JSONArray data_2 = (JSONArray)result.get("data");
        JSONObject data_2 = (JSONObject)result.get("data");
        UartTmp uartTmp=JSONObject.toJavaObject(data_2,UartTmp.class);
        if(ObjectUtil.isNotEmpty(uartTmp)){
            saveUartTmp(uartTmp);
        }
        log.info("************synchPmsProject************结束");
        return AjaxResult.success();
    }

    /**
     * 存储设备告警信息
     * @param uartTmp
     */
    private void saveUartTmp(UartTmp uartTmp){
        try {
            uartTmp.setAlarmTime(DateUtil.parse(uartTmp.getDatetime()));
            uartTmp.setCreateTime(new Date());
            uartTmp.setMark("0");
            uartTmpService.saveUartTmp(uartTmp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 解密校验
     * @param requestParam
     * @param castFlag 批量数据转换标识 单个：true,批量false
     * @return
     */
    private static AjaxResult decodeVerify(JSONObject requestParam,boolean castFlag){
        if(requestParam == null ){
            return AjaxResult.error("信息为空");
        }
        String dataAES_2 = null;
        boolean flag = false;
        try {
            //解密参数
            Integer pid_2 = requestParam.getInteger("pid");
            String channelId_2 = requestParam.getString("channelId");
            dataAES_2 = requestParam.getString("data");
            Long timestamp_2 = requestParam.getLong("timestamp");
            //检查签名
            Map<String, String> signParam_2 = new HashMap<>();
            signParam_2.put("pid", pid_2.toString());
            signParam_2.put("channelId", channelId_2);
            signParam_2.put("timestamp", timestamp_2.toString());
            signParam_2.put("data", dataAES_2);
            flag = EncryptUtil.Encrypt.checkSignSHA1(signParam_2, requestParam.getString("sign"), EncryptUtil.PUBLIC_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!flag) {
            log.info("调用签名错误");
            return AjaxResult.error("签名错误");
        }
        //aes解密
        if(castFlag){
            JSONObject data_2 = JSONObject.parseObject(EncryptUtil.decryptAES(EncryptUtil.AES_KEY, dataAES_2));
            log.info("解密后的data:" + data_2.toString());
            return AjaxResult.success(data_2);
        }else{
            JSONArray array = JSONUtil.parseArray(EncryptUtil.decryptAES(EncryptUtil.AES_KEY, dataAES_2)) ;
            log.info("解密后的dataList:" + array.toString());
            return AjaxResult.success(array);
        }
    }
}
